package com.valuegroup.smarttodo.ui.todo.habit;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.valuegroup.smarttodo.R;
import com.valuegroup.smarttodo.common.BaseViewHolder;
import com.valuegroup.smarttodo.common.CommonRecyclerAdapter;
import com.valuegroup.smarttodo.data.ServiceFactory;
import com.valuegroup.smarttodo.data.model.AppStatus;
import com.valuegroup.smarttodo.data.model.HabitEvent;
import com.valuegroup.smarttodo.data.model.HabitOrder;
import com.valuegroup.smarttodo.data.model.Remark;
import com.valuegroup.smarttodo.plugins.pickdialog.DateUtils;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddActivity;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddEditText;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QucikAddInputCheckRule;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QuickAddEditorCallback;
import com.valuegroup.smarttodo.ui.todo.task.qucikadd.QuickAddHolder;
import com.valuegroup.smarttodo.util.AlertUtils;

import java.util.Date;
import java.util.List;

public class HabitSigninAdapter extends CommonRecyclerAdapter<HabitEvent> {


    public HabitSigninAdapter(Context context, List<HabitEvent> data) {
        super(context, data);
        setMainLayoutId(R.layout.item_habit_signin);
    }

    @Override
    public void bindHeadView(BaseViewHolder v, int position) {

    }

    @Override
    public void bindBottomView(BaseViewHolder v, int position) {

    }

    @Override
    public void bindMainView(BaseViewHolder v, int position) {
        HabitEvent data = getData(position);
        final Remark remark = ServiceFactory.getInstance().getRemarkService().getRemark(AppStatus.RemarkType.HABIT_SIGNIN, data.getId());
        final HabitOrder order = ServiceFactory.getInstance().getHabitService().getHabitOrderDao().load(data.getOid());

        TextView labScheduDt = v.getView(R.id.labScheduDt);
        TextView signDt = v.getView(R.id.signDt);
        TextView btnSupplement = v.getView(R.id.btnSupplement);
        TextView tagFinish = v.getView(R.id.tagFinish);
        TextView editRemark = v.getView(R.id.editRemark);
        TextView labOrderTitle = v.getView(R.id.labOrderTitle);
        ImageView btnClearRemark = v.getView(R.id.btnClearRemark);
        btnSupplement.setVisibility(View.GONE);
        editRemark.setVisibility(View.VISIBLE);
        signDt.setVisibility(View.VISIBLE);
        tagFinish.setVisibility(View.GONE);
        labScheduDt.setText(DateUtils.date2str(data.getScheduleDt(), DateUtils.F31));
        labScheduDt.setTextColor(ContextCompat.getColor(getmContext(), R.color.textDefault));

        btnSupplement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 补卡事件
                HabitEvent event = ServiceFactory.getInstance().getHabitService().doClockInDateForHabitEvent(data.getId());
                refreshData(position, event);
            }
        });

        btnClearRemark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertUtils.delDialog(getmContext(), R.string.remark, (dialog, which) -> {
                    ServiceFactory.getInstance().getRemarkService().clearRemark(AppStatus.RemarkType.HABIT_SIGNIN, data.getId());
                    notifyItemChanged(position, data);
                });
            }
        });

        editRemark.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 编辑内容
                QucikAddActivity.openEditorNoRepeat(
                        getmContext(),
                        new QuickAddEditorCallback() {
                            @Override
                            public void onCancel() {
                            }

                            @Override
                            public void onSubmit(String content) {
                                if (TextUtils.isEmpty(content)) {
                                    return;
                                }
                                ServiceFactory.getInstance().getRemarkService().saveRemark(AppStatus.RemarkType.HABIT_SIGNIN, data.getId(), content);
                                notifyItemChanged(position, data);
                            }

                            @Override
                            public void onAttached(final ViewGroup rootView) {
                                // 表情(暂时弃用)
                                QucikAddEditText etContent = rootView.findViewById(R.id.quick_add_task_content_edt);
                                if (remark != null) {
                                    etContent.setText(remark.getContent());
                                }
                            }
                        },
                        new QuickAddHolder(QuickAddHolder.QuickAddType.HABIT_SIGNING),
                        new QucikAddInputCheckRule(120, 2));
            }
        });

        if (order != null) {
            labOrderTitle.setText(order.getTitle());
        } else {
            labOrderTitle.setText("历史数据");
        }

        if (remark != null && !TextUtils.isEmpty(remark.getContent())) {
            editRemark.setText(remark.getContent());
            btnClearRemark.setVisibility(View.VISIBLE);
        } else {
            editRemark.setText("");
            btnClearRemark.setVisibility(View.GONE);
        }

        if (DateUtils.compareDate(new Date(), data.getScheduleDt()) > 0) {
            // 过去
            if (data.getFinish()) {
                signDt.setText(DateUtils.date2str(data.getClockInDate(), DateUtils.F13));
                tagFinish.setVisibility(View.VISIBLE);
            } else {
                // 前三天可以补卡
                if (DateUtils.compareDate(DateUtils.moveDay(-1), data.getScheduleDt()) == 0
                        || DateUtils.compareDate(DateUtils.moveDay(-2), data.getScheduleDt()) == 0
                        || DateUtils.compareDate(DateUtils.moveDay(-3), data.getScheduleDt()) == 0) {
                    btnSupplement.setVisibility(View.VISIBLE);
                    signDt.setVisibility(View.GONE);
                } else {
                    signDt.setText(getmContext().getResources().getString(R.string.habit_deficiency));
                }
            }
        } else if (DateUtils.compareDate(new Date(), data.getScheduleDt()) == 0) {
            // 当天
            if (data.getFinish()) {
                signDt.setText(DateUtils.date2str(data.getClockInDate(), DateUtils.F13));
                tagFinish.setVisibility(View.VISIBLE);
            } else {
                signDt.setVisibility(View.GONE);
            }
        } else {
            // 未来
            signDt.setVisibility(View.GONE);
            editRemark.setVisibility(View.GONE);
        }


        // 标记当天
        if (DateUtils.compareDate(new Date(), data.getScheduleDt()) == 0) {
            labScheduDt.setTextColor(ContextCompat.getColor(getmContext(), R.color.colorToday));
        }

        // 标记周六周日
        if (labScheduDt.getText().toString().contains("星期日") || labScheduDt.getText().toString().contains("星期六")) {
            labScheduDt.setTextColor(ContextCompat.getColor(getmContext(), R.color.colorWeekend));
        }
    }

    @Override
    public void bindAppendView(BaseViewHolder v, int position) {

    }
}
